package com.zupig.drawwatchdemo.CView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by Leegl on 2017/5/15 0015.
 */

public class CircleViw  extends View{
    public CircleViw(Context context) {
        super(context);
    }

    public CircleViw(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleViw(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
       WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        super.onDraw(canvas);
        /////////////////////////////////////////////////////////////////////////////////////////////
        //画圆形
        Paint paintCircle = new Paint();
        paintCircle.setAntiAlias(true);
        paintCircle.setStyle(Paint.Style.STROKE);
        paintCircle.setStrokeWidth(5);
        canvas.drawCircle(width/2,height/2,width/2-100,paintCircle);
        //画刻度
        Paint paintDegree = new Paint();
        paintDegree.setAntiAlias(true);
        paintDegree.setStyle(Paint.Style.STROKE);
        paintDegree.setStrokeWidth(3);
       for (int i =0;i<24;i++){
           //区分整点
           if (i == 0||i==6||i==12||i==18){
               paintDegree.setStrokeWidth(5);
               paintDegree.setTextSize(30);
               canvas.drawLine(width/2,height/2-width/2+100,width/2,height/2-width/2+120,paintDegree);
               String degree = String.valueOf(i);
               canvas.drawText(degree,width/2-paintDegree.measureText(degree)/2,height/2-width/2+140,paintDegree);
           }else {
               paintDegree.setStrokeWidth(3);
               paintDegree.setTextSize(15);
               canvas.drawLine(width/2,height/2-width/2+100,width/2,height/2-width/2+120,paintDegree);
               String degree = String.valueOf(i);
               canvas.drawText(degree,width/2-paintDegree.measureText(degree)/2,height/2-width/2+140,paintDegree);
           }
           //通过旋转画布简化坐标运算
           canvas.rotate(15,width/2,height/2);
       }
        //画指针
        Paint paintHour = new Paint();
        paintHour.setAntiAlias(true);
        paintHour.setStrokeWidth(20);
        Paint paintMinute = new Paint();
        paintMinute.setAntiAlias(true);
        paintMinute.setStrokeWidth(10);
        canvas.save();
        //将坐标系原点移动到圆心位置
        canvas.translate(width/2,height/2);
        canvas.drawLine(0,0,100,100,paintHour);
        canvas.drawLine(0,0,100,200,paintMinute);
        canvas.restore();
    ////////////////////////////////////////////////////////////////////////////////////////////////
        //layer 图层的测试
        Paint mPaint = new Paint();
        //这是一个新的图层的开始，如果把画布设置成其他颜色，那么就会覆盖到原来的画布上面，导致之前的内容被遮盖，而不显示
        canvas.drawColor(Color.TRANSPARENT);

        mPaint.setColor(Color.BLUE);
        canvas.drawCircle(100,100,100,mPaint);

        canvas.saveLayerAlpha(0,0,400,400,127,Canvas.FULL_COLOR_LAYER_SAVE_FLAG);
        mPaint.setColor(Color.RED);
        canvas.drawCircle(200,200,100,mPaint);
        canvas.restore();
    }
}
